<?php

namespace app\admin\model;

//use app\common\traits\Gate;
use app\common\traits\Text;
use think\Cache;
use think\Model;

/**
 * 用户模型类
 * Class Users
 * @package app\admin\model
 */
class Users extends Model
{
    use Text;


//    use Gate;
    protected $updateTime = 'last_login_time';
    protected $createTime = 'register_time';
    protected $update = ['last_login_ip'];
//    protected $autoWriteTimestamp = 'int';
    protected $autoWriteTimestamp = true;

    protected $type = [
        'last_login_time' => 'timestamp',
        'register_time' => 'timestamp',
    ];

    /**
     * 返回用户的用户名
     * @param $uid
     * @return mixed
     */
    public function getNickName($uid)
    {
        return $this->where(array('id' => (int)$uid))->value('username');
    }


    /**
     * 获得所有的状态（从缓存中）
     * @return false|mixed|\PDOStatement|string|\think\Collection   返回数组
     */
    public function getAllStates(){
        if (!empty(Cache::get('allStates'))){
            return Cache::get('allStates');
        }else{
            $states = State_model::all();
            Cache::set('allStates',$states);
            return $states;
        }
    }

    /**
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getPositionTextAttr($value, $data){
        // 动态获取器
        $state_logic = new Position();
        $status = collect($state_logic->getAllPosition())->column('title', 'id');
        return $status[$data['position']];
    }

    /**信息启用状态
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getStatusTextAttr($value, $data){
        $status = [
            0 => "<span class=\"label label-danger\">禁用</span>",
            1 => "<span  class=\"label label-success\">启用</span>",
        ];
        return $status[$data['status']];
    }

    /**
     * @param $value
     * @param $data
     * @return mixed
     */
    public function getWeixinstatusTextAttr($value, $data)
    {
        $status = [1 => '<span class="label label-success">已绑定</span>', 2 => '<span  class="label label-danger">未绑定</span>'];
        return $status[$data['weixinstatus']];
    }

    /**
     *编辑测试
     */
    public function edit()
    {
        halt($this->checkRule());
    }

    /**
     * 自动填写ip
     * @return mixed
     */
    protected function setLastLoginIpAttr()
    {
        return request()->ip();
    }

    //获取用户角色
    public function juese()
    {
        return $this->belongsToMany('AuthGroup', 'auth_group_access', 'group_id', 'uid');
    }


    //获取用户部门
    public function depar()
    {
        return $this->belongsTo('depar', 'depar_id', 'id', [], "LEFT");
    }

    //获取用户部门
    public function positions()
    {
        return $this->belongsTo('Position', 'position', 'id', [], "LEFT");
    }

    //获取用户地区
    public function dq()
    {
        return $this->belongsTo('dq', 'dq_id', 'id', [], "LEFT");
    }

    //获取用户组
    public function group()
    {
        return $this->belongsTo('group', 'group_id', 'id', [], "LEFT");
    }

    //获取邀约分组
    public function ygroup()
    {
        return $this->belongsTo('ygroup', 'ygroup_id', 'id', [], "LEFT");
    }

    /**
     * 微信用户登录
     * @param array $users 用户名
     * @param bool $rememberme 记住登录
     * @return bool|mixed
     */
    public function wechatlogin($users, $rememberme = false)
    {
        $map['openid'] = $users['id'];
        $user = self::get($map);
        $id = $user['id'];
        // 更新登录信息
        $user['counts'] = $user['counts'] + 1;
        if ($user->save()) {
            // 自动登录
            $this->auto_login(self::get($id), $rememberme);
            return $id;
        } else {
            return false;
        }

    }
}